package export.generator;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import elements.Deal;
import elements.Shop;
import elements.User;
import export.db.Loader;

public class Generator {
	private Loader loader;
	
	public Document getString(List<Deal> deals) {
		Document document = DocumentHelper.createDocument();
		Element dealsE = document.addElement("deals");
		for (Deal deal: deals) {
			Element dealE = dealsE.addElement("deal");
			
			Element websiteE = dealE.addElement("website");
			websiteE.setText(deal.getWebsite());
			
			Element oidE = dealE.addElement("oid");
			oidE.setText(deal.getOid());
			
			Element titleE = dealE.addElement("title");
			titleE.setText(deal.getTitle());
			
			Element dealUrlE = dealE.addElement("dealUrl");
			dealUrlE.setText(deal.getDealUrl());
			
			Element sImageUrlE = dealE.addElement("sImageUrl");
			sImageUrlE.setText(deal.getSImageUrl());
			
			Element mImageUrlE = dealE.addElement("mImageUrl");
			mImageUrlE.setText(deal.getMImageUrl());
			
			Element lImageUrlE = dealE.addElement("lImageUrl");
			lImageUrlE.setText(deal.getLImageUrl());
			
			Element cityNameE = dealE.addElement("cityName");
			cityNameE.setText(deal.getCityName());
			
			Element categoryE = dealE.addElement("category");
			categoryE.setText(deal.getCategory());
			
			Element tipsE = dealE.addElement("tips");
			tipsE.setText(deal.getTips());
			
			Element priceE = dealE.addElement("price");
			priceE.setText(Double.toString(deal.getPrice()));
			
			Element dealValueE = dealE.addElement("dealValue");
			dealValueE.setText(Double.toString(deal.getDealValue()));
			
			Element discountPercentE = dealE.addElement("discountPercent");
			discountPercentE.setText(Double.toString(deal.getDiscountPercent()));
			
			Element startTimeE = dealE.addElement("startTime");
			startTimeE.setText(Long.toString(deal.getStartTime()));
			
			Element endTimeE = dealE.addElement("endTime");
			endTimeE.setText(Long.toString(deal.getEndTime()));
			
			Element dealDescE = dealE.addElement("dealDesc");
			dealDescE.setText(deal.getDealDesc()==null?"":deal.getDealDesc());
			
			Element salesNumE = dealE.addElement("members");
			salesNumE.setText(Integer.toString(deal.getSalesNum()));
			
			Element shopsE = dealE.addElement("shops");
			for (Shop shop: deal.getShops()) {
				Element shopE = shopsE.addElement("shop");
				
				Element shopUrlE = shopE.addElement("shopUrl");
				shopUrlE.setText(shop.getShopUrl() == null?"":shop.getShopUrl());
				
				Element shopNameE = shopE.addElement("shopName");
				shopNameE.setText(shop.getShopName() == null?"":shop.getShopName());
				
				Element shopPhoneE = shopE.addElement("shopPhone");
				shopPhoneE.setText(shop.getShopPhone() == null?"":shop.getShopPhone());
				
				Element shopAddressE = shopE.addElement("shopAddress");
				shopAddressE.setText(shop.getShopAddress() == null?"":shop.getShopAddress());
				
				Element shopAreaE = shopE.addElement("shopArea");
				shopAreaE.setText(shop.getShopArea() == null?"":shop.getShopArea());
				
				Element longtitudeE = shopE.addElement("longtitude");
				longtitudeE.setText(Double.toString(shop.getLongtitude()));
				
				Element latitudeE = shopE.addElement("latitude");
				latitudeE.setText(Double.toString(shop.getLatitude()));
			}
			
		}
		return document;
	}
	
	public Document getStringUser(User user) {
		Document document = DocumentHelper.createDocument();
		Element userE = document.addElement("user");
		
		Element userIdE = userE.addElement("userId");
		userIdE.setText(user.getUserId());
		
		Element typeIdE = userE.addElement("type");
		typeIdE.setText(user.getType() == null?"":user.getType());
		
		Element websiteE = userE.addElement("website");
		websiteE.setText(user.getWebsite() == null?"":user.getWebsite());
		
		Element titleE = userE.addElement("title");
		titleE.setText(user.getTitle() == null?"":user.getTitle());
		
		Element endTimeE = userE.addElement("endTime");
		endTimeE.setText(Integer.toString(user.getEndTime()));
		
		Element cityNameE = userE.addElement("cityName");
		cityNameE.setText(user.getCityName() == null?"":user.getCityName());
		
		Element priceE = userE.addElement("price");
		priceE.setText(Double.toString(user.getPrice()));
		
		Element discountPercentE = userE.addElement("discountPercent");
		discountPercentE.setText(Double.toString(user.getDiscountPercent()));
		
		Element updateTimeE = userE.addElement("updateTime");
		updateTimeE.setText(Long.toString(user.getUpdateTime()));
		
		Element salesNumE = userE.addElement("members");
		salesNumE.setText(Integer.toString(user.getSalesNum()));
		
		Element distanceE = userE.addElement("distance");
		distanceE.setText(Integer.toString(user.getDistance()));
		
		return document;
	}
}
